package com.leozhang.app;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import java.util.stream.*;

//TIP 要<b>运行</b>代码，请按 <shortcut actionId="Run"/> 或
// 点击装订区域中的 <icon src="AllIcons.Actions.Execute"/> 图标。
public class Main {

    private static final String hello() {
        String s = new String("Hello");
        return s;
    }

    public static void main(String[] args) {

        User user = new User();
        user.setName("Leo");
        user.setAge(25);
        String strUser = user.toString();
        System.out.println(strUser);

        String filePath = "/Users/zhangchengfei/tmp/user.txt";
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(filePath);
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(user);
            objectOutputStream.close();
            fileOutputStream.close();
            System.out.println("User 对象已序列化到 " + filePath + " 文件中");
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 反序列化
        try {
           FileInputStream fileInputStream = new FileInputStream(filePath);
            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
            User user1 = (User) objectInputStream.readObject();
            objectInputStream.close();
            fileInputStream.close();
            System.out.println("User 对象已反序列化");
            System.out.println("反序列化的对象：" + user1);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }



        System.out.println("---------------------------");
        String a = "Hello";
        String b = a.repeat(3);
        System.out.println(b);
        System.out.println("---------------------------");

        Long userId = null;
        List<Long> userIds = Collections.singletonList(userId);
        System.out.println(userIds);
        System.out.println("----------------------------");


        System.out.println("Available processors: " + Runtime.getRuntime().availableProcessors());
//        Runtime.getRuntime().gc();
        System.out.println("Total memory: " + Runtime.getRuntime().totalMemory() / 1024 / 1024 + " MB");
        System.out.println("Max memory: " + Runtime.getRuntime().maxMemory() / 1024 / 1024 + " MB");
        System.out.println("Free memory: " + Runtime.getRuntime().freeMemory() / 1024 / 1024 + " MB");

//        Stream.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "j").forEach(System.out::println);
//        Stream.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "j").parallel().forEach(System.out::println);


//        Demo demo = new Demo();
//        demo.test();

        System.out.println("----------------------------");
        new Random(47).ints(5, 20)
                .distinct()
                .limit(7)
                .sorted()
                .forEach(System.out::println);


        Random random = new Random(47);
        SortedSet<Integer> rints = new TreeSet<>();
        while (rints.size() < 7) {
            int r = random.nextInt(20);
            if (r < 5)
                continue;
            rints.add(r);
        }
        System.out.println("----------------------------");
        System.out.println(rints);

        List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
        System.out.println("----------------------------");
        list.stream().filter(i -> i % 2 == 0).forEach(System.out::println);
        Map<Boolean, List<Integer>> map = list.stream().collect(Collectors.groupingBy(i -> i % 2 == 0));
        System.out.println("*----------------------------*");
        map.get(true).forEach(System.out::println);
        map.get(false).forEach(System.out::println);

        if (list.stream().anyMatch(i -> i % 2 == 0)) {
            System.out.println("list中存在偶数");
        }

        int[] arr = {-100, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        if (Arrays.stream(arr).anyMatch(s -> s < 0)) {
            System.out.println("数组中存在负数");
        }
        //TIP 当文本光标位于高亮显示的文本处时按 <shortcut actionId="ShowIntentionActions"/>
        // 查看 IntelliJ IDEA 建议如何修正。
//        System.out.print("Hello and welcome!\n");
//
//        List<String> originalList = Arrays.asList("a", "b", "a", "c", "b");
//        System.out.println(originalList);
//        List<String> uniqueList = originalList.stream().distinct().collect(Collectors.toList());
//        System.out.println(uniqueList);
//
//        Integer m = 127;
//        int n = 127;
//        System.out.println("i == j: " +  (m == n));
//        System.out.println("i == j: " + m.equals(n));
//
//        m = 129;
//        n = 129;
//        System.out.println("-----------------------");
//        System.out.println("i == j: " +  (m == n));
//        System.out.println("i == j: " + m.equals(n));
//
//        String a = "Hello";
//        String b = hello();
//        System.out.println("a==b: " + (a == b));
//
//        Map<String, List<String>> userRoleMap = new HashMap<>();
//        if (userRoleMap == null) {
//            System.out.println("userRoleMap is null");
//        }
//
//        String tmpStr =  "";
//        String[] roleNames = new String[]{"超级管理员", "部门与员工管理员", "实训中心管理员"};
//        for (int i = 0; i < roleNames.length; i++) {
//            tmpStr += "“" + roleNames[i] + (i == roleNames.length - 1 ? "”" : "”、");
//        }
//        System.out.println(tmpStr);
//
//
//        for (int i = 1; i <= 5; i++) {
//            //TIP 按 <shortcut actionId="Debug"/> 开始调试代码。我们已经设置了一个 <icon src="AllIcons.Debugger.Db_set_breakpoint"/> 断点
//            // 但您始终可以通过按 <shortcut actionId="ToggleLineBreakpoint"/> 添加更多断点。
//            System.out.println("i = " + i);
//        }
//
//        System.out.println("HELLO, WORLD");
//        Person p = new Person();
//        p.setName("HELLO");
//        System.out.println("Get my name: " + p.getName());
//
//        Person p2 = new Person();
//        try {
//            ObjectOutputStream objectOutputStream = new ObjectOutputStream(
//                    Files.newOutputStream(Paths.get("/Users/zhangchengfei/tmp/person1.txt")));
////            ObjectOutputStream objectOutputStream = new ObjectOutputStream(
////                    new FileOutputStream("/Users/zhangchengfei/tmp/person2.txt"));
//            objectOutputStream.writeObject(p2);
//            objectOutputStream.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }
}
